#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
    // const char * filename = "./test.txt";
    // int fd = open(filename,O_CREAT);

    // const char * filename = "./reademe.txt";
    // int fd = open(filename,O_RDONLY);

    const char *filename = "./test.txt";
    int fd1 = open(filename, O_RDONLY | O_CREAT, 0644); // 77
    // 绿色是可执行权限 chmod
    // 0644 (8进制)

    if (fd1 == -1)
    {
        perror("open error:");
        exit(-1);
    }
    // 0(标准输入) 1(标准输出) 2(标准出错)
    printf("fd1 = %d\n", fd1);

    /* 绝对路径 pwd*/  
    const char* filename2 = "dst.txt";
    int fd2 = open(filename2,O_WRONLY | O_CREAT,0777);
    if(fd2 == -1)
    {
        perror("open error:");
        exit(-1);
    }
    printf("fd2 = %d\n", fd2);

    // 关闭文件fd1
    close(fd1);  

    const char* filename3 = "file3.txt";
    int fd3 = open(filename3,O_WRONLY | O_CREAT,0777);
    if(fd3 == -1)
    {
        perror("open error:");
        exit(-1);
    }
    printf("fd3 = %d\n", fd3);
    
    return 0;
}
